package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.TeachPlanService;
import com.xuecheng.module.dto.SaveTeachplanDto;
import com.xuecheng.module.dto.TeachPlanDto;
import com.xuecheng.module.po.Teachplan;
import com.xuecheng.module.po.TeachplanMedia;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 星河一叶Ck
 * @date 2025/4/22
 */

@Service
@RequiredArgsConstructor
@Slf4j
public class TeachPlanServiceImpl implements TeachPlanService {
    private final TeachplanMapper teachplanMapper;
    private final TeachplanMediaMapper teachplanMediaMapper;

    /**
     * 根据课程id查询课程计划
     * @param courseId 课程id
     * @return 课程计划
     */
    @Override
    public List<TeachPlanDto> getTreeNodes(long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    /**
     * 新增/修改课程计划
     * @param saveTeachplanDto 课程计划信息
     */

    @Transactional
    @Override
    public void saveTeachplan(SaveTeachplanDto saveTeachplanDto) {
        // 获取课程计划id
        Long id = saveTeachplanDto.getId();
        // 若id为空 则不存在该课程计划 就是新增 否则是修改
        if (id != null) {
            // 修改
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(saveTeachplanDto, teachplan);
            teachplanMapper.updateById(teachplan);
        } else {
            // 新增
            // 取出同父级别的课程计划数量
            Integer count = getTeachPlanCount(saveTeachplanDto.getCourseId(), saveTeachplanDto.getParentid());
            Teachplan teachplanNew = new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto, teachplanNew);
            teachplanNew.setOrderby(count + 1);
            teachplanMapper.insert(teachplanNew);
        }
    }

    /**
     * 删除课程计划
     * @param id 课程计划id
     */
    @Transactional
    @Override
    public void deleteTeachPlan(Long id) {
        // 查询课程计划
        Teachplan teachplan = teachplanMapper.selectById(id);
        // 判断是大章节还是小节
        if (teachplan.getGrade() == 1) {
            // 如果是大章节 判断下面是否有小节 若有 则提示提交异常
            Integer count = teachplanMapper.getSmallCourseCount(id);
            if (count > 0) {
                throw new XueChengPlusException(120409,"课程计划信息还有子级信息，无法操作");
            }
            teachplanMapper.deleteById(id);

        } else if (teachplan.getGrade() == 2) {
            // 删除关联的媒体信息
            teachplanMediaMapper.deleteByTeachplanId(id);
            // 删除小节
            teachplanMapper.deleteById(id);
        }
    }

    // 取出同父级别的课程计划数量
    private int getTeachPlanCount (long courseId, long parentId) {
        LambdaQueryWrapper<Teachplan> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Teachplan::getParentid, parentId);
        lqw.eq(Teachplan::getCourseId, courseId);
        return teachplanMapper.selectCount(lqw);
    }
}
